package tv.youxing.android.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.kymjs.rxvolley.http.VolleyError;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import tv.youxing.android.MainActivity;
import tv.youxing.android.R;
import tv.youxing.android.custom.CustomToast;
import tv.youxing.android.entity.UserPwdLoginModel;
import tv.youxing.android.ui.SetPasswordActivity;
import tv.youxing.android.ui.login.LoginApi;
import tv.youxing.android.ui.login.OnLoginListener;
import tv.youxing.android.ui.login.UserInfo;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.L;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;
import tv.youxing.android.utils.UtilTools;

/**
 * Created by apple on 2017/4/24.
 */

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .fragment
 * 文件名：   UserPwdLoginFragment
 * 创建者：   叶克武
 * 创建时间： 2017/4/24 下午2:36
 * 描述：    TODO
 */
public class UserPwdLoginFragment extends BaseFragment{


    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.password)
    EditText password;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_pwd_login, null);
        ButterKnife.bind(this, view);
        return view;
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.btn_userpwd_login, R.id.tvForgetPwd, R.id.qq_login, R.id.weixin_login, R.id.weibo_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_userpwd_login:





                userOrPwdlogin();
//                List<EditText> editTexts = new ArrayList<>();
//                editTexts.add(phone);
//                editTexts.add(password);
//
//                if (!UtilTools.isEditTextListNot(editTexts)) {
//                    CustomToast.INSTANCE.showToast("账号或密码错误");
//                    return;
//                }
                

                break;
            case R.id.tvForgetPwd:
                startActivity(new Intent(getActivity(), SetPasswordActivity.class));
                break;
            case R.id.qq_login:
                login(QQ.NAME,1);
                break;
            case R.id.weixin_login:
                login(Wechat.NAME,2);
                break;
            case R.id.weibo_login:
                login(SinaWeibo.NAME,3);
                break;
        }
    }



    /**
     * 账号密码登录
     */
    private void userOrPwdlogin() {



        String mobile = UtilTools.getString(phone);
        String pwd = UtilTools.getString(password);


        if (mobile.equals("") || pwd.equals("")) {
            CustomToast.INSTANCE.showToast("请正确输入");
            return;
        }


        showProgress("加载中");



        //post请求简洁版实现
        HttpParams params = new HttpParams();
        params.put("mobile",mobile);
        params.put("credential",pwd);
        params.put("login_type","1");


//        string:mobile=  #手机号码
//        string:credential=  # 密码
//        string:code=  # 短信验证码
//int:login_type=  # 登录类型 1 密码登录 2验证码登录

        RxVolley.post(StaticClass.userLoginURL, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                UserPwdLoginModel userPwdLoginModel = GsonUtils.GsonToBean(t, UserPwdLoginModel.class);

                if (userPwdLoginModel.getCode() == 200) {

                    System.out.println("token:"+userPwdLoginModel.getResult().getToken());


                    L.d("authid:" + userPwdLoginModel.getResult().getIs_authenticated());

                    ShareUtils.putString(getContext(), AppConst.TOKEN,userPwdLoginModel.getResult().getToken());
                    ShareUtils.putString(getContext(), AppConst.USERNAME,userPwdLoginModel.getResult().getUsername());
                    ShareUtils.putInt(getContext(), AppConst.USERID,userPwdLoginModel.getResult().getUser_id());
                    ShareUtils.putString(getContext(), AppConst.AVATER,userPwdLoginModel.getResult().getAvatar());
                    ShareUtils.putBoolean(getContext(),AppConst.ISLOGIN,true);

//                    ReservoirUtils.putString(AppConst.TOKEN,userPwdLoginModel.getResult().getToken());

                    Intent intent = new Intent(getActivity(), MainActivity.class);


                    startActivity(intent);

                    getActivity().finish();

                } else {
//                    CustomToast.INSTANCE.showToast("登录失败:"+userPwdLoginModel.getCode());
                    CustomToast.INSTANCE.showToast("登录失败:"+userPwdLoginModel.getStatus());
                }


                hideProgress();
            }

            @Override
            public void onFailure(VolleyError error) {
                super.onFailure(error);
                L.d("onFailure:"+error.getMessage());
                hideProgress();
            }
        });
    }



    void login(String name,int type) {
            LoginApi api = new LoginApi();
            //设置登陆的平台后执行登陆的方法
            api.setPlatform(name);
            api.setOnLoginListener(new OnLoginListener() {
                public boolean onLogin(String platform, HashMap<String, Object> res) {
                    // 在这个方法填写尝试的代码，返回true表示还不能登录，需要注册
                    // 此处全部给回需要注册
                    return true;
                }

                public boolean onRegister(UserInfo info) {
                    // 填写处理注册信息的代码，返回true表示数据合法，注册页面可以关闭
                    return true;
                }
            });
            api.login(getContext(),type);
        }
    }
